package com.yg.base.controller;


import com.yg.base.pojo.Image;
import com.yg.base.pojo.vo.ImageVo;
import com.yg.base.service.ImageService;
import com.yg.util.Result;
import com.yg.util.StatusCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 图片表 前端控制器
 * </p>
 *
 * @author 魏成龙
 * @since 2020-11-07
 */
@RestController
@RequestMapping("/base/image")
public class ImageController {

    @Autowired
    private ImageService imageService;

    /**
     * 查询所有轮播数据
     * @return
     */
    @GetMapping("getBanner")
    public Result getItem(){
        List<Image> allBanner = imageService.findAllBanner();
        return new Result(true,StatusCode.OK,"查询成功",allBanner);
    }

    /**
     * 根据targetId查询图片url
     */
    @GetMapping("getImageUrl/{id}")
    public Result getImageUrl(@PathVariable Long id){
        Image image = imageService.getById(id);
        if (image==null){
            return new Result(false,StatusCode.ERROR,"查询失败",null);
        }
        String imgUrl = image.getImgUrl();

        return new Result(true,StatusCode.OK,"查询成功",imgUrl);
    }

    /**
     * 查询所有图片
     */
    @GetMapping("getAllImage")
    public List<ImageVo> getAllImage(){
        List<Image> list = imageService.list();

        List<ImageVo> images = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Image image = list.get(i);
            ImageVo imageVo = new ImageVo();
            imageVo.setImageUrl(image.getImgUrl());
            imageVo.setTargetId(image.getTargetId());
            images.add(imageVo);
        }
        return images;

    }

}
